//
//  MianViewController.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/11.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "MainTabViewController.h"
#import "HomeViewController.h"
#import "PersonalViewController.h"
#import "UIView+WLExtension.h"
#import "RoomViewController.h"


@interface MainTabViewController ()

@property (nonatomic,strong)UIButton *roomBtn;

@property (nonatomic,strong)RoomViewController *roomViewController;

@end

@implementation MainTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor blackColor];
    
    [self setupChildVC];
    [self setupChildItemProperties];
    
    CGRect originFrame = self.tabBar.frame;
//    originFrame
    
    [self.tabBar addSubview:self.roomBtn];
    [self.tabBar bringSubviewToFront:self.roomBtn];
//    originFrame.size.height -= 10;
//    self.tabBar.frame = originFrame;
//    self.tabBar.WL_height -= 10;
//    self.tabBar.WL_y += 10;
}


- (void)setupChildVC{
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    UITabBarItem *homeItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage  imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home"]];
    homeItem.image = [[UIImage imageNamed:@"home_origin"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeItem.selectedImage = [[UIImage imageNamed:@"home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeVC.tabBarItem = homeItem;
    
    

    PersonalViewController *personalVC = [[PersonalViewController alloc]init];
    UITabBarItem *personalItem = [[UITabBarItem alloc]initWithTitle:@"个人中心" image:[UIImage  imageNamed:@"personal"] selectedImage:[UIImage imageNamed:@"personal"]];
    personalItem.image = [[UIImage imageNamed:@"personal_origin"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    personalItem.selectedImage = [[UIImage imageNamed:@"personal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    personalVC.tabBarItem = personalItem;

    
//    UIViewController *blankVC = [[UIViewController alloc]init];
//    UITabBarItem *blankItem = [[UITabBarItem alloc]initWithTitle:@"直播" image:[UIImage  imageNamed:@""]selectedImage:[UIImage imageNamed:@""]];
//    blankVC.tabBarItem = blankItem;

    
    
    UINavigationController *personalNav = [[UINavigationController alloc]initWithRootViewController:personalVC];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:homeVC];
//    UINavigationController *blankNav = [[UINavigationController alloc]initWithRootViewController:blankVC];
//    [self.navigationController setNavigationBarHidden:YES];
    self.viewControllers = [NSArray arrayWithObjects:homeNav,personalNav, nil];
}

- (void)setupChildItemProperties{
    
    UITabBarItem *homeItem = [self.tabBar.items firstObject];
    UITabBarItem *personalItem = [self.tabBar.items lastObject];
    homeItem.title = @"首页";
    personalItem.title = @"个人中心";
}


#pragma mark event response
- (void)roomButtonClicked:(UIButton *)button{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL user_login_status = [defaults boolForKey:@"user_login_status"];
    if (!user_login_status) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注意" message:@"请先执行登录操作，才可以进行直播喔！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES
                         completion:nil];
    }else{
        _roomViewController = [[RoomViewController alloc]init];
        [self presentViewController:self.roomViewController animated:YES completion:nil];
    }
    
    //    [self showDetailViewController:self.roomViewController sender:self];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIButton *)roomBtn{
    if (!_roomBtn) {
        _roomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _roomBtn.backgroundColor = [UIColor colorWithRed:60/255.f green:157/255.f blue:189/255.f alpha:1.f];
        UITabBar *tab = self.tabBar;
        CGRect frame = tab.frame;
        frame.origin.y =  - 12;
        frame.size.width = tab.frame.size.height+8;//tab.frame.size.width/3.f;
        frame.size.height = frame.size.width;
        _roomBtn.frame = frame;
        
        _roomBtn.WL_centerX = tab.WL_centerX;
        
        
        _roomBtn.layer.cornerRadius = frame.size.width/2;
        _roomBtn.layer.masksToBounds = YES;
        
        _roomBtn.titleLabel.font = [UIFont systemFontOfSize:12.f];
        UIColor *tintColor = [UIColor colorWithRed:191/255.f green:116/255.f blue:52/255.f alpha:1.f];
        
        [_roomBtn setTitleColor:tintColor forState:UIControlStateNormal];
        [_roomBtn setTitle:@"我要直播" forState:UIControlStateNormal];
        [_roomBtn addTarget:self action:@selector(roomButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _roomBtn;
}

//- (RoomViewController *)roomViewController{
//    if (!_roomViewController) {
//        _roomViewController = [[RoomViewController alloc]init];
//    }
//    return _roomViewController;
//}

@end
